/*
 * -------------------------------------------------------------------------------------------------
 *                            COPYRIGHT(C) BOCO HEALTH 2016
 * -------------------------------------------------------------------------------------------------
 *
 * =================================================================================================
 *  HISTORY
 *
 *      Tag                Date            Author           Description
 * =================================================================================================
 *      created            2016/11/17      lizhineng        create
 * =================================================================================================
 *
 */

package com.boco.base.utils;

import com.boco.base.App;
import com.boco.base.http.ApiService;
import com.boco.base.retrofit.Interceptors.CacheInterceptor;
import com.boco.base.retrofit.Interceptors.LogInterceptor;
import com.google.gson.Gson;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 创建人： caodailu
 * 创建时间：2016/9/9 0009
 * 邮箱：caodailu@boco.com.cn
 */
public class RetrofitUtils {
    private static final int READ_TIMEOUT = 10;//读取超时时间,单位  秒
    private static final int CONN_TIMEOUT = 2;//连接超时时间,单位  秒
    private static File cacheFile = new File(App.getInstance().getExternalCacheDir(), "ccCache");
    private static Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
    private static Retrofit mRetrofit;

    private RetrofitUtils() {

    }

    public static Retrofit newInstance() {
        mRetrofit = null;
        Interceptor log = new LogInterceptor();
        Interceptor cacheInterceptor = new CacheInterceptor(0, 60 * 60 * 24 * 7);
        //设置无网络读取缓存，有网络访问最新数据，无网络的情况下缓存存活7天
        OkHttpClient client = new OkHttpClient.Builder()//初始化一个client,不然retrofit会自己默认添加一个
                .connectTimeout(CONN_TIMEOUT, TimeUnit.SECONDS)//设置连接时间为2s
                .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)//设置读取时间为10s
                .addInterceptor(log)
                .addInterceptor(cacheInterceptor)
                .addNetworkInterceptor(cacheInterceptor)
                .cache(cache)
                .build();

        mRetrofit = new Retrofit.Builder()
                .client(client)//添加一个client,不然retrofit会自己默认添加一个
                .baseUrl(ApiService.TEST_SERVER)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        return mRetrofit;
    }

    /**
     * 带缓存时间段的实例化
     *
     * @param maxAge   缓存时间段
     * @param maxStale 超出超时期间
     * @return
     */
    public static Retrofit newInstance(int maxAge, int maxStale) {
        mRetrofit = null;
        Interceptor log = new LogInterceptor();
        Interceptor cacheInterceptor = new CacheInterceptor(maxAge, maxStale);

        OkHttpClient client = new OkHttpClient.Builder()//初始化一个client,不然retrofit会自己默认添加一个
                .connectTimeout(CONN_TIMEOUT, TimeUnit.SECONDS)//设置连接时间为12s
                .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)//设置读取时间为60s
                .addInterceptor(log)
                .addInterceptor(cacheInterceptor)
                .addNetworkInterceptor(cacheInterceptor)
                .cache(cache)
                .build();

        mRetrofit = new Retrofit.Builder()
                .client(client)//添加一个client,不然retrofit会自己默认添加一个
                .baseUrl(ApiService.TEST_SERVER)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        return mRetrofit;
    }

//    @NonNull
//    public static RequestBody map2JsonBody(Map map) {
//        String json = new Gson().toJson(map);
//        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
//    }

    public static RequestBody object2JsonBody(Object object) {
        String json = new Gson().toJson(object);
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
    }
}